package cn.edu.hqu.cst.lzs.homework4;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    private static final String TAG = "MyService";
    private int count = 0;
    private boolean quit = false;
    private final MyServiceBinder myServiceBinder = new MyServiceBinder();

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"Service is binded");
        return myServiceBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG,"Service is created");
        new Thread(() -> {
            while(!quit){
                try{
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MyService.this.count++;
            }
        }).start();
    }

    class MyServiceBinder extends Binder {
        public int getCount() {
            Log.d(TAG,"onStartCommand");
            return MyService.this.count;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        boolean quit = true;
        Log.d(TAG,"Service is Destroyed");
    }
}